using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using usm.SolDev.DataBase.DataObjects;
using System.Diagnostics;

namespace usm.SolDev.UI
{
    public partial class EditorForm : DockContent
    {
        #region Private properties
        private ProjectDescriptor projectDescriptor;
        #endregion

        #region Public properties

        /// <summary>
        ///     The project descriptors holds the sate of the project. Each time
        /// a project is returned, it's object state is modified according to internal 
        /// variables. When getting the project, the program field is updated 
        /// automatically.
        /// </summary>
        public ProjectDescriptor Project
        {
            get
            {
                // TODO probably should delete the text below
                //if (solDevEditor.Changed == true && projectDescriptor.State == usm.SolDev.Common.Config.ObjectState.Original)
                //{
                //    projectDescriptor.State = usm.SolDev.Common.Config.ObjectState.Modified;
                //}

                //if (solDevEditor.Changed == true)
                //{
                //    projectDescriptor.LastEditTime = DateTime.Now;
                //}

                projectDescriptor.Program = solDevEditor.Text;
                return projectDescriptor; 
            }

            set
            { 
                projectDescriptor = value;
                solDevEditor.Text = projectDescriptor.Program;
            }
        }
        #endregion

        #region Public methods

        /// <summary>
        ///     Seek and select required line in code. Wrap to components 
        /// seek to line method.
        /// </summary>
        /// <param name="lineNumber">the line number to seek to</param>
        /// <param name="select">the flag which indicates if the line should be 
        /// selected after</param>
        public void SeekToLine(int lineNumber, int offset, int length, bool select)
        {
            // TODO not sure if this will work
            int position = solDevEditor.Lines[lineNumber].StartPosition;
            solDevEditor.Selection.Start = position + (offset == -1 ? 0 : offset);
            solDevEditor.Selection.Length = length == -1 ? 0 : length;

            //solDevEditor.SeekToLine(lineNumber, offset, length, select);
        }
        #endregion

        #region Constructors

        /// <summary>
        ///     Default constructor
        /// </summary>
        public EditorForm()
        {
            InitializeComponent();

            if (DesignMode == true)
            {
                return;
            }

            #region Setup editor content

            solDevEditor.ConfigurationManager.CustomLocation = "sol.xml";
            solDevEditor.ConfigurationManager.Language = "vb";
            solDevEditor.Text = "";
            TabText = "New";
            projectDescriptor = new ProjectDescriptor();
            #endregion
        }

        /// <summary>
        ///     Load project descriptor
        /// </summary>
        /// <param name="project"></param>
        public EditorForm(ProjectDescriptor project)
        {
            InitializeComponent();

            if (DesignMode == true)
            {
                return;
            }

            #region Setup editor content
            solDevEditor.ConfigurationManager.CustomLocation = @"sol.xml";
            solDevEditor.ConfigurationManager.Language = "vb";
            if (project.State == usm.SolDev.Common.Config.ObjectState.New)
            {
                TabText = "New";
            }
            else
            {
                TabText = project.Description;
            }

            projectDescriptor = project;
            solDevEditor.Text = projectDescriptor.Program;
            #endregion
        }
        #endregion

        #region Private properties
        ///  Indicates if the current view is being utilized in the VS.NET
        ///  IDE or not. The similarly-named .Net framework property for 
        ///  a UserControl will only show that its related object in 
        ///  is DesignMode only if the immediate parent is viewed in the IDE. 
        ///  
        ///  For instance, if UserControl A has UserControlB placed on it,
        ///  and UserControl B has UserControlC placed on it, 
        ///  and UserControl A is being viewed in the IDE, 
        ///  UserControl C will normally register its DesignMode flag as false. 
        ///  This overridden implementation of DesignMode will utilized a 
        ///  different method in determining this.
        protected static new bool DesignMode
        {
            get
            {
                return (
                        Process.GetCurrentProcess().ProcessName.Equals("VCSExpress") ||
                        Process.GetCurrentProcess().ProcessName.Equals("devenv")
                    );
            }
        }
        #endregion

    }
}

